package com.example.clock;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class AddDataDialog extends Dialog {
    private Context context;
    private OnButtonClickListener listener;
    private EditText department;
    private EditText workTime;
    private Button cancel;
    private Button ok;

    public void setListener(OnButtonClickListener listener) {
        this.listener = listener;
    }

    public AddDataDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public AddDataDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected AddDataDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.add_dialog);

        department = (EditText) findViewById(R.id.department);
        workTime = (EditText) findViewById(R.id.workTime);
        cancel = (Button) findViewById(R.id.cancel);
        ok = (Button) findViewById(R.id.ok);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener!=null){
                    try {
                        listener.onOkClicked(department.getText().toString(),Integer.parseInt(workTime.getText().toString()));
                    }catch (Exception e){
                        Toast.makeText(context,workTime.getText().toString()+"==> ???",Toast.LENGTH_LONG).show();
                    }
                }
            }
        });
    }

    public interface OnButtonClickListener{
        void onOkClicked(String department,int workTime);
    }
}
